package Control;


import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


import Entity.MsgPackage;

import GUI.Login;
import GUI.ScreenManager;
import Entity.User;


public class LoginControl {
	private Login logInPanel;
	private MsgPackage LonInMsg;
	private User user;
	private Object result;
	
	public LoginControl(){
		logInPanel = new Login();
		LonInMsg= new MsgPackage();
		user= new User();
		result=null;
	}
	
	public User getUser() {
		return user;
	}

	public Login getlogInPanel(){
		return logInPanel;
	}

	public void log_out_pressed(Integer user_name){
		LonInMsg.setObject(user_name);
		LonInMsg.setOp_code(OP_CODE.LOG_OUT);
		ScreenManager.getConnection().accept(LonInMsg);
		
		//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
    	//Object o = ScreenManager.getConnection().getResultobject();
		Object o = ScreenManager.getConnection().cclient.getMessage();

	}

	public boolean loginReq_pressed(){
		String x = logInPanel.getTextArea_userName().getText();
		String y = logInPanel.getPasswordField().getText();
		if(x.equals("") || y.equals("")){
			JOptionPane.showMessageDialog(null, "Not all the fileds are filled", "ERROR", JOptionPane.ERROR_MESSAGE);

		return false;	
		}
		
		else{
			try {
				user.setUserName(Integer.parseInt(x));
			} catch( NumberFormatException e) {
				
			}
		user.setPassword(y);
		LonInMsg.setObject(user);
		LonInMsg.setOp_code(OP_CODE.LOGIN);
		ScreenManager.getConnection().accept(LonInMsg);
		
		//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
		Object o = ScreenManager.getConnection().cclient.getMessage();

    	//Object o = ScreenManager.getConnection().getResultobject();
    	result = (Integer)o;
    	
    	if((Integer)result==0){
    		JOptionPane.showMessageDialog(null, "User does not exsits!", "ERROR", JOptionPane.ERROR_MESSAGE);	
    	     return false;}
    	else if((Integer)result==1){
    		JOptionPane.showMessageDialog(null, "You are allready logged!", "ERROR", JOptionPane.ERROR_MESSAGE);
    		return false;}
    	else if ((Integer)result==2){
			LonInMsg.setOp_code(OP_CODE.GET_PERMISSIONS);
			ScreenManager.getConnection().accept(LonInMsg);

			//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
			Object w = ScreenManager.getConnection().cclient.getMessage();

			//Object w = ScreenManager.getConnection().getResultobject();
	    	result = (User)w;

		

	    	user=(User) result;
	    	ScreenManager.getUserName().setUserName(user.getUserName());
	    	ScreenManager.getUserName().setRole(user.getRole());
	    	ScreenManager.getUserName().setReq_auto_list(user.getReq_auto_list());
	    	ScreenManager.getUserName().setReq_list(user.getReq_list());
		}
		
		}
		return true;
		}
	
}